#ifndef __MANAGER_H__
#define __MANAGER_H__
/////////////////
//include
/////////////////
#include"../CXBurst/Array.h"
#include"../CXBurst/List.h"
#include"Model.h"
/////////////////
//const
/////////////////

/////////////////
//class
/////////////////
class Device;
class DeviceContext;
template<class MANAGE_TYPE>
class Manager : public List<MANAGE_TYPE>
{
public:
	Manager(int object_num) : List<MANAGE_TYPE>()
	{
		models = Array<Model*>();
	}
	~Manager()
	{
		if(models.GetReference() == 1)
		{
			for(int i = 0;i<models.GetSize();i++)
			{
				delete models[i];
			}
		}
	}
	virtual void Init()
	{
		SetFirst();
		ListMember* tmp_list = last;
		while (tmp_list != NULL)
		{
			ListMember* tmp_list_member = tmp_list->GetPre();
			if(tmp_list_member == NULL)
			{
				tmp_list->Delete();
				return;
			}
			tmp_list->Delete();
			tmp_list = tmp_list_member;
		}
	}
	virtual	void ContentLoad(Device* device) = 0;
	virtual void Update()
	{
		SetFirst();
		ListMember* tmp_list = Get();
		while (tmp_list != NULL)
		{
			UpdateFunc(tmp_list->GetObj());
			tmp_list = Get();
		}
	}
	virtual void UpdateFunc(MANAGE_TYPE* type) = 0;
	virtual void Draw(DeviceContext* device_context)
	{
		SetFirst();
		ListMember* tmp_list = Get();
		while (tmp_list != NULL)
		{
			DrawFunc(device_context,tmp_list->GetObj());
			tmp_list = Get();
		}
	}
	virtual	void DrawFunc(DeviceContext* device_context, MANAGE_TYPE* type) = 0;
	void Set(MANAGE_TYPE* object,int model_index)
	{
		SetFunc(object,model_index);
		List::Set(object);
	}
	virtual	void SetFunc(MANAGE_TYPE* object,int model_index) = 0;
	void Delete()
	{
		if(last == NULL)
			return;
		SetFirst();
		ListMember* tmp_list = last;
		ListMember* tmp_list_member = tmp_list->GetPre();
		while (tmp_list != NULL)
		{
			
			if(tmp_list_member == NULL&&DeleteFunc(tmp_list->GetObj()))
			{
				tmp_list->Delete();
				return;
			}
			if(tmp_list_member == NULL)
				return;
			if(DeleteFunc(tmp_list_member->GetObj()))
			{
				ListMember* tmp = tmp_list_member->GetPre();
				tmp_list_member->Delete();
				tmp_list_member = tmp;
			}
			else
			{
				tmp_list_member = tmp_list_member->GetPre();
			}
			
		}
	}
virtual	bool DeleteFunc(MANAGE_TYPE* object) = 0;
protected:
	Array<Model*>	models;
};
#endif __MANAGER_H__
